﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.DAL;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;
using System.Xml;

namespace Touch2Like.Web.Customer.Popup
{
    public partial class AddQuestion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["ListID"]))
                {
                    QuestionDAL question = new QuestionDAL();
                    QuestionInfo questionInfo = question.GetByID(Convert.ToInt32(Request["ListID"]));

                    if (questionInfo != null)
                    {
                        ddl_QuestionType.SelectedValue = questionInfo.QuestionType;

                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(questionInfo.Question);

                        tb_Question.Text = doc.SelectSingleNode("/Q/Question").InnerText;

                        string vals = "";
                        foreach (XmlNode node in doc.SelectNodes("/Q/Item"))
                        {
                            vals += node.InnerText + "~~~";
                        }

                        tb_Options.Value = vals;
                    }

                    if (Request["IsRemove"] == "true")
                    {
                        ddl_QuestionType.Enabled = false;
                        tb_Question.ReadOnly = true;
                        btn_Submit.Visible = false;
                        btn_Remove.Visible = true;
                        lb_Remove.Visible = true;
                    }
                }
            }
        }

        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            QuestionDAL question = new QuestionDAL();
            QuestionInfo questionInfo = new QuestionInfo();
            if (!string.IsNullOrEmpty(Request["ListID"]))
            {
                questionInfo = question.GetByID(Convert.ToInt32(Request["ListID"]));

                if (questionInfo != null)
                {
                    questionInfo.QuestionType = ddl_QuestionType.SelectedValue;
                    questionInfo.UserGUID = WebUtility.UserGUID;
                    questionInfo.Question = tb_Question.Text;

                    string q = "<Question>" + tb_Question.Text + "</Question>";
                    if (ddl_QuestionType.SelectedValue == "MultiCheck" || ddl_QuestionType.SelectedValue == "SingleCheck")
                    {
                        string[] qs = tb_Options.Value.Split(new string[] { "~~~" }, StringSplitOptions.None);
                        foreach (string qi in qs)
                        {
                            if (qi.Trim() != "")
                            {
                                q += "<Item>" + qi + "</Item>";
                            }
                        }
                    }
                    q = "<Q>" + q + "</Q>";

                    questionInfo.Question = q;
                    question.Update(questionInfo);
                }
            }
            else
            {
                questionInfo.QuestionType = ddl_QuestionType.SelectedValue;
                questionInfo.UserGUID = WebUtility.UserGUID;
                questionInfo.Question = tb_Question.Text;

                string q = "<Question>" + tb_Question.Text + "</Question>";
                if (ddl_QuestionType.SelectedValue == "MultiCheck" || ddl_QuestionType.SelectedValue == "SingleCheck")
                {
                    string[] qs = tb_Options.Value.Split(new string[] { "~~~" }, StringSplitOptions.None);
                    foreach (string qi in qs)
                    {
                        if (qi.Trim() != "")
                        {
                            q += "<Item>" + qi + "</Item>";
                        }
                    }
                }
                q = "<Q>" + q + "</Q>";

                questionInfo.Question = q;
                question.Add(questionInfo);
            }

            ClientScript.RegisterStartupScript(this.GetType(), "update", "<script>parent.document.location.href=parent.document.location.href;parent.document.location.reload();</script>");
        }

        protected void btn_Remove_Click(object sender, EventArgs e)
        {
            QuestionDAL question = new QuestionDAL();
            if (!string.IsNullOrEmpty(Request["ListID"]))
            {
                question.Delete(Convert.ToInt32(Request["ListID"]));
            }
            else
            {
                return;
            }

            ClientScript.RegisterStartupScript(this.GetType(), "remove", "<script>parent.document.location.href=parent.document.location.href;parent.document.location.reload();</script>");

        }
    }
}